import fetch from "node-fetch";
import puppeteer from './mypuppeteer.js'

import md5 from 'md5'


var rooms = [
	[952211 , '拉姆' , [1161070838 , 780460510] , '开播了，快来围观吧\nhttps://live.bilibili.com/'] ,
	[27711199 , '琦琦' , [780460510] , '开播了，快来围观吧\nhttps://live.bilibili.com/'] ,
	[5105847 , '西瓜' , [1161070838] , '这鸽子还知道开播？赶紧先给琦琦清崩铁！\nhttps://live.bilibili.com/'] ,
	[27951735 , '人美心善的骨老师!!' , [1161070838 , 780460510] , '开播了，快来围观吧\nhttps://live.bilibili.com/'], 
    [30161743 , '麻烦' , [780460510] , '开播了，大家不要去看\nhttps://live.bilibili.com/'] ,
    [25621100 , '又香又软又大的菜菜' , [780460510] , '开播了，快来围观吧\nhttps://live.bilibili.com/']
] ;

export class stplus extends plugin {
  constructor () {
    super({
      name: '直播监控',
      dsc: '#直播监控',
      event: 'message',
      rule: [
        {
          reg: '#直播监控开启',
          fnc: 'zbjk'
        } ,
		{
          reg: '#直播监控关闭',
          fnc: 'zbjkClose'
        } , 
		{
          reg: '#直播状态重置',
          fnc: 'resetLiveStatus'
        }
      ]
    })
	/** 定时任务 */
    this.task = {
      cron: "1 * * * * ?",
      name: '直播监控',
      fnc: () => this.sendToGroup()
    }

  }

  async sendToGroup() {
	let cfg = await redis.get("liveMonitor");  
	if(cfg != 1){
		console.log("监控配置未开启");
		return ;
	}
	
	for(let i = 0 ; i < rooms.length ;  i++){
		var liveStatus = await this.getStatus(rooms[i][0]) ;
		var lastLiveStatus = await redis.get("liveStatus:" + rooms[i][0]);
		if(liveStatus != lastLiveStatus){
			await redis.set("liveStatus:" + rooms[i][0] , liveStatus);
			if(liveStatus ==1 ){
				for(let g = 0 ; g < rooms[i][2].length ; g++){
                    console.log(rooms[i][2] ) ;
                    console.log(rooms[i][2][g] ) ;
					let groupId = rooms[i][2][g] ;
					await Bot.pickGroup(groupId).sendMsg(rooms[i][1]+rooms[i][3]+rooms[i][0]);
				}
			} else {
				for(let g = 0 ; g < rooms[i][2].length ; g++){
                    console.log(rooms[i][2] ) ;
                    console.log(rooms[i][2][g] ) ;
					let groupId = rooms[i][2][g] ;
					console.log("向群"+groupId+"发送消息:"+rooms[i][1]+"下播了") ;
				}
			}
		}
	}
  }

  async resetLiveStatus(){
	if (!this.e.isMaster) {
      this.e.reply('只有管理员可用...')
      return false
    }
	for(let i = 0 ; i < rooms.length ;  i++){
		await redis.set("liveStatus:" + rooms[i][0] , 0);
	}
	
	await this.reply("直播状态已重置") ;
  }

  async zbjk () {
	if (!this.e.isMaster) {
      this.e.reply('只有管理员可用...')
      return false
    }
	await redis.set("liveMonitor" , 1);
	await this.reply("直播监控已开启") ;
  }

  async zbjkClose () {
	if (!this.e.isMaster) {
      this.e.reply('只有管理员可用...')
      return false
    }
	await redis.set("liveMonitor" , 0);
    await this.reply("直播监控已关闭") ;
  }

  async getStatus(roomId) {
	
	let url = 'https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?protocol=0,1&format=0,1,2&codec=0,1&qn=0&platform=web&ptype=8&dolby=5&panorama=1&room_id=' + roomId ; 
	let res = await fetch(url).then(res => res.json());
	
	return res.data.live_status ;

  }
}
